package designPattern.state
{
	import button.MyButton;
	
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.text.TextField;
	import flash.text.TextFieldType;

	/**
	 * 状态模式的特点：
	 * #状态作为对象的部分存在于对象中。 
	 * #当状态变化是，对象会在某些方面变化。表面上看，对象可能改变了类，但实际上，他们改变了类里的行为.
	 * #每个对象的行为取决于对象中其他状态的当前状态。
	 * @author a
	 * 
	 */	
	public class StateMainClass
	{
		private var nc:NetConnection = new NetConnection();
		private var ns:NetStream;
		private var vid:Video;
		private var works:Works;
		private var playBtn:MyButton;
		private var stopBtn:MyButton;
		private var pauseBtn:MyButton;
		private var flv:String;
		private var flv_txt:TextField;
		private var dummy:Object;
		private var root:Stage;
		
		public function StateMainClass(rt:Stage)
		{
			this.root = rt;
			nc.connect(null);
			ns = new NetStream(nc);
			
			vid = new Video(528, 300);
			vid.x = (root.stageWidth - vid.width)/2;
			vid.y = (root.stageHeight - vid.height)/2 - 120;
			root.addChild(vid);
			
			//创建状态机实例
			works = new Works();
			
			//按钮
			playBtn = new MyButton("Play");
			stopBtn = new MyButton("Stop");
			pauseBtn = new MyButton("Pause");
			playBtn.x = (root.stageWidth - playBtn.width)/2 - 100;
			playBtn.y = 350;
			stopBtn.x = (root.stageWidth - stopBtn.width)/2;
			stopBtn.y = 350;
			pauseBtn.x = (root.stageWidth - pauseBtn.width)/2 + 100;
			pauseBtn.y = 350;
			
			root.addChild(playBtn);
			root.addChild(stopBtn);
			root.addChild(pauseBtn);
			
			//添加事件监听
			playBtn.addEventListener(MouseEvent.CLICK, doPlay);
			stopBtn.addEventListener(MouseEvent.CLICK, doStop);
			pauseBtn.addEventListener(MouseEvent.CLICK, doPause);
			
			//添加文本域
			flv_txt = new TextField();
			flv_txt.text = "q7_1";
			flv_txt.border = true;
			flv_txt.borderColor = 0x9e0039;
			flv_txt.background = true;
			flv_txt.backgroundColor = 0xfab383;
			flv_txt.type = TextFieldType.INPUT;
			flv_txt.x = root.stageWidth/2 - 45;
			flv_txt.y = 10;
			flv_txt.width = 90;
			flv_txt.height = 16;
			root.addChild(flv_txt);
			
			//放置元数据错误信息被扔掉
			dummy = {};
			ns.client = dummy;
			dummy.onMetaData = getData;
			
			ns.addEventListener(NetStatusEvent.NET_STATUS, flvCheck);
		}
		
		private function flvCheck(evt:NetStatusEvent):void
		{
			switch(evt.info.code)
			{
				case "NetStream.Play.Stop":
					works.startStop(ns);
					vid.clear();
					break;
				case "NetStream.Play.StreamNotFound":
					works.startStop(ns);
					flv_txt.text = "File not found";
					break;
			}
		}
		
		private function getData(meta:Object):void
		{
			trace(meta.duration);	
		}
		
		private function doPlay(evt:MouseEvent):void
		{
			if(flv_txt.text != "" && flv_txt.text != "Provide file name")
			{
				flv_txt.textColor = 0x000000;
				flv = flv_txt.text + ".flv";
				works.startPlay(ns, flv);
				vid.attachNetStream(ns);
			}
			else
			{
				flv_txt.textColor = 0xcc0000;
				flv_txt.text = "Provide file name";
			}
		}
		
		private function doStop(evt:MouseEvent):void
		{
			works.startStop(ns);
			vid.clear();
		}
		
		private function doPause(evt:MouseEvent):void
		{
			works.doPause(ns);
		}
	}
}